Function exercise with Matplotlib

Here we want to chart answers to questions.

Below is code that will show a barchart for one question.

Create a function to do this so the data in the third cell can be used to output a barcharts for each.


In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

In [ ]:
title_text = 'Happiness'
important = [0,0,4,3,10,21]
current = [1,1,16,10,8,2]

N = 6
ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(ind, important, width, color='r')
rects2 = ax.bar(ind + width, current, width, color='y')
ax.set_ylabel('Reponses')
title = ax.set_title(title_text)
fig.tight_layout()
title.set_y(1.05)
fig.subplots_adjust(top=0.8)

ax.set_xticks(ind + width / 2)
ax.set_xticklabels(('0', '1', '2', '3', '4', '5'))
ax.legend((rects1[0], rects2[0]), ('Important', 'Current'), loc='upper left')
plt.show()
# fig.savefig('output.png') # Save to disk

In [ ]:
title = 'Happiness'
important = [0,0,4,3,10,21]
current =[1,1,16,10,8,2]
filename = 'Q1'

title = 'Health'
important = [3,0,4,7,16,8]
current =[0,4,16,11,5,2]
filename = 'Q2'

title = 'Love'
important = [0,1,3,9,15,10]
current =[0,3,17,11,7,0]
filename = 'Q3'

title = 'Career'
important = [0,2,4,5,16,11]
current =[0,9,12,11,6,0]
filename = 'Q4'

title = 'Family'
important = [0,1,2,7,13,15]
current =[0,9,10,10,7,2]
filename = 'Q5'